<?php

namespace app\admin\controller\yjqrcode;

use app\common\controller\Backend;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Debug;

/**
 * 产品列管理
 *
 * @icon fa fa-circle-o
 */
class Item extends Backend
{

    /**
     * Item模型对象
     * @var \app\admin\model\yjqrcode\Item
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\yjqrcode\Item;
        $this->qrcodemodel = new \app\admin\model\yjqrcode\Qrcode;

    }

    /**
     * 生成产品编号
     */
    public function makecode()
    {
        $id = request()->param('id', 0, 'intval');
        $num = request()->param('num', 0, 'intval');
        if ($id == 0 || $num == 0) {
            return $this->error("参数不正确，请重试");
        }
        $item = $this->model::where('id', $id)->find();
        if ($item && !empty($item['coderule'])) {
            $_coderule = str_split($item['coderule']);

            $codes = $this->qrcodemodel::where('item_id', $item['id'])->column('code');
            $codestr = $codes ? implode(',', $codes) : ',';

            $i = 0;
            $list = [];
            while ($i < $num) {
                $_code = $this->randcode($_coderule);
                if (strpos($_code, $codestr) === false) {
                    $codestr .= ',' . $_code;
                    $list[] = ['code' => $_code, 'item_id' => $item['id'], 'createtime' => time()];
                    $i++;
                } else {
                    echo $_code . PHP_EOL;
                }
            }

            $rut = db('yjqrcode_qrcode')->insertAll($list); //返回成功条数
            if ($rut) {
                $this->model::where('id', $id)->setInc('nums', $rut);
                return $this->success("操作完成");
            }
        }
        return $this->error("产品不存在或者没有设置编号规则");
        // dump($item);
    }

    /**
     * 根据编号规则随机一条编号
     */
    public function randcode($rule)
    {
        $code = '';
        foreach ($rule as $k) {
            if ($k == 'n') {
                $code .= mt_rand(0, 9);
            } else {
                $code .= $k;
            }
        }
        return $code;
    }

    /**
     * 导出
     */
    public function export()
    {
        set_time_limit(0);
        $id = request()->param("id", 0, 'intval');
        $data = db("yjqrcode_qrcode")->where("item_id", $id)->column('code');

        header('Content-Type: application/vnd.ms-excel;charset=gbk');
        header('Content-Disposition: attachment;filename="' . time() . '.csv"');
        header('Cache-Control: max-age=0');
        $file = 'php://output';
        $fp = fopen($file, 'a');
        fputcsv($fp, [iconv('utf-8', 'gbk', '二维码编码url')]);
        $_url = addon_url('yjqrcode/index/index', ['sn' => ''], true, true);
        foreach ($data as $v) {
            fputcsv($fp, [iconv('utf-8', 'gbk', $_url . $v)]);
        }
        ob_flush();
        flush();
        fclose($fp);
        return;
    }
}
